<template>
  <div>
     <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
     <!-- 搜索添加区域 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getProductList">
          <el-button slot="append" icon="el-icon-search" @click="getProductList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
          <el-button type="primary" @click="goAddProduct">添加商品</el-button>
      </el-col>
    </el-row>
    <!-- 商品列表区域 -->
    <el-table border stripe :data="productList">
      <el-table-column label="#" type="index"></el-table-column>
      <el-table-column label="商品名称" prop="label"></el-table-column>
      <el-table-column label="商品价格(元)" prop="price"></el-table-column>
      <el-table-column label="商品数量" prop="num"></el-table-column>
      <el-table-column label="创建时间" prop="created_time">
        <template slot-scope="scope">
          {{ scope.row.created_time | dataFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button v-model="scope.row._id" type="primary" icon="el-icon-edit" size="mini" ></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deletePro(scope.row._id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      queryInfo: {
        query: ''
      },
      productList: []
    }
  },
  created () {
    this.getProductList()
  },
  methods: {
    async getProductList () {
      const res = await this.$http.get('/prolist')
      console.log(res)
      this.productList = res.data
    },
    goAddProduct () {
      this.$router.push('/goods/add')
    },
    async deletePro (id) {
      const result = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result !== 'confirm') {
        return this.$message.info('已取消该操作')
      }
      await this.$http.delete('/delPro/' + id)
      this.$message.success('删除成功！')
      this.getProductList()
    }
  }
}
</script>
<style lang="less" scoped></style>
